package com.lite.business.service.retailer.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lite.business.convert.retailer.RetailerConvert;
import com.lite.business.entity.retailer.Retailer;
import com.lite.business.entity.retailer.RetailerQueryWrapper;
import com.lite.business.exception.BusinessException;
import com.lite.business.mapper.retailer.RetailerMapper;
import com.lite.business.service.retailer.RetailerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.cache.annotation.Caching;
import org.springframework.http.HttpStatus;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author stranger
 * @since 2022-10-25
 */
@Service
public class RetailerServiceImpl extends ServiceImpl<RetailerMapper, Retailer> implements RetailerService {


    @Autowired
    RetailerMapper retailerMapper;

    @Autowired
    RetailerConvert retailerConvert;

    @Override
    @Cacheable(value = "getRetailerById", key = "#retailerId")
    public Retailer getRetailerById(Integer retailerId) throws BusinessException {
        Retailer retailer = retailerMapper.selectOne(new LambdaQueryWrapper<Retailer>().eq(Retailer::getId, retailerId));
        if (Objects.isNull(retailer)) {
            throw new BusinessException(HttpStatus.INTERNAL_SERVER_ERROR.value(), "零售商信息查询失败");
        }

        return retailer;
    }

    @Override
    @Cacheable(value = "listRetailerByPage")
    public List<Retailer> listRetailerByPage(Integer page, Integer size, RetailerQueryWrapper retailerQueryWrapper) throws BusinessException {
        List<Retailer> retailers = page(new Page<>(page, size), retailerQueryWrapper.buildWrapper()).getRecords();
        if (Objects.isNull(retailers)) {
            throw new BusinessException(HttpStatus.INTERNAL_SERVER_ERROR.value(), "零售商列表信息查询失败");
        }
        return retailers;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @Caching(
            evict = {
                    @CacheEvict(value = "getRetailerById", key = "#retailerId"),
                    @CacheEvict(value = "listRetailerByPage", allEntries = true)
            }
    )
    public Boolean updateRetailerInfo(Integer retailerId, Retailer newRetailer) throws BusinessException {
        int count = retailerMapper.updateById(newRetailer);
        if (count <= 0) {
            throw new BusinessException(HttpStatus.INTERNAL_SERVER_ERROR.value(), "更新零售商信息失败");
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @Caching(
            evict = {
                    @CacheEvict(value = "getRetailerById", key = "#retailerId"),
                    @CacheEvict(value = "listRetailerByPage", allEntries = true)
            }
    )
    public Boolean removeRetailerInfo(Integer retailerId) throws BusinessException {
        int count = retailerMapper.deleteById(retailerId);
        if (count <= 0) {
            throw new BusinessException(HttpStatus.INTERNAL_SERVER_ERROR.value(), "删除零售商信息失败");
        }
        return true;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    @Caching(
            evict = {
                    @CacheEvict(value = "getRetailerById", key = "#retailer.id"),
                    @CacheEvict(value = "listRetailerByPage", allEntries = true)
            }
    )
    public Boolean addRetailerInfo(Retailer retailer) throws BusinessException {
        int count = retailerMapper.insert(retailer);
        if (count <= 0) {
            throw new BusinessException(HttpStatus.INTERNAL_SERVER_ERROR.value(), "新增零售商失败");
        }
        return true;
    }
}
